package cn.hs.ph.servlet;


import cn.hs.ph.dao.CourierStationDao;
import cn.hs.ph.dao.impl.CourierStationDaoImpl;
import cn.hs.ph.entity.CourierStation;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/courierStation")
public class CourierStationServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //2.获取action，判断
        String action = req.getParameter("action");
        CourierStationDao courierStationDao = new CourierStationDaoImpl();
        if ("selectAll".equals(action)) {
            //3.获取请求信息
            String communityName = req.getParameter("communityName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String courierStationName = req.getParameter("courierStationName");
            //封装到map集合中
            Map<String, String> map = new HashMap<>();
            map.put("communityName", communityName);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("courierStationName", courierStationName);
            //连接数据库
            List<CourierStation> list = courierStationDao.selectAll(map);
            int total = courierStationDao.selectCount(map);
            //给出响应
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("add".equals(action)) {
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String id = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId = 0;
            if (id != null && !"".equals(id)) {
                courierStationCommunityId = Integer.parseInt(id);
            }
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationProvince = req.getParameter("courierStationProvince");
            CourierStation courierStation = new CourierStation(courierStationAddress, courierStationCity, courierStationCommunityId, courierStationDistrict, courierStationImage, courierStationName, courierStationProvince);
            int num = courierStationDao.insert(courierStation);
            Result result = null;
            if (num != 0) {
                result = Result.success("添加成功");
            } else {
                result = Result.error("添加失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        } else if("modify".equals(action)){
            String courierStationAddress = req.getParameter("courierStationAddress");
            String courierStationCity = req.getParameter("courierStationCity");
            String id = req.getParameter("courierStationCommunityId");
            int courierStationCommunityId = 0;
            if (id != null && !"".equals(id)) {
                courierStationCommunityId = Integer.parseInt(id);
            }
            String courierStationDistrict = req.getParameter("courierStationDistrict");
            String courierStationImage = req.getParameter("courierStationImage");
            String courierStationName = req.getParameter("courierStationName");
            String courierStationProvince = req.getParameter("courierStationProvince");
            CourierStation courierStation = new CourierStation(courierStationAddress, courierStationCity, courierStationCommunityId, courierStationDistrict, courierStationImage, courierStationName, courierStationProvince);
            int num = courierStationDao.update(courierStation);
            Result result = null;
            if (num != 0) {
                result = Result.success("修改成功");
            } else {
                result = Result.error("修改失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        } else if ("delete".equals(action)) {
            String Id = req.getParameter("courierStationId");
            int courierStationId = 0;
            if (Id != null && !"".equals(Id)) {
                courierStationId = Integer.parseInt(Id);
            }
            int num = courierStationDao.delete(courierStationId);
            Result result = null;
            if (num != 0) {
                result = Result.success("删除成功");
            } else {
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
